#include "Game.h"
#include "GrabbingEnemy.h"
#include "KnifeThrowingEnemy.h"

#include "NumericalBitmapFont.h"

Game* Game::instance = NULL;

Game::Game()
{
	window = new Window(320,240, "I Know Kung Fu", 2);
	player = new Player();
	
	instance = this;
}

void Game::go()
{
    TestLevel l;
    HUD h;


	SpawnInfo info;
	info.x = 50;

    entities.push_back(player);
	entities.push_back(new GrabbingEnemy(info));
	info.x = 300;
	entities.push_back(new KnifeThrowingEnemy(info));
	//entities.push_back(new AxeThrowingNinja());

	bool pressOk = true;

    while (window->isOpen())
    {
		if( Keyboard::isKeyPressed(SDLK_ESCAPE) ) return;

		if(Keyboard::isKeyPressed(SDLK_e) == false) pressOk = true;
		if( pressOk && Keyboard::isKeyPressed(SDLK_e) )
		{
			entities.push_back(new GrabbingEnemy(info));
			pressOk = false;
		}

        // Update entities
        for(list<Entity*>::iterator i = entities.begin(); i != entities.end(); )
        {
			Entity* e = *i;
			list<Entity*>::iterator deleter = i;

			// Increment iterator here (as we can't once we've removed the entity from the list)
			i++;

			if( e->hasExpired()){
				// Remove expired entities
				if(e != player) delete e;

				// Remove from list
				entities.erase(deleter);
			}else{
				// Tick non expired entities
				e->maybeTick();
			}	
        }

		Viewport::calcOffsets();
         // Draw the level
        l.draw();

        // Draw entities
        for(list<Entity*>::iterator i = entities.begin(); i != entities.end(); i++)
        {
            (*i)->draw();
        }

        // Draw the Heads Up Display
        h.draw();

        // Let the window do it's stuff
        window->process();

		// Sleep
        SDL_Delay(10);
    }
}
